package org.jeecg.modules.zhima.portal.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.zhima.admin.entity.ZmPlayerRanking;
import org.jeecg.modules.zhima.admin.service.IZmPlayerRankingService;
import org.jeecg.modules.zhima.portal.domain.Player;
import org.jeecg.modules.zhima.portal.domain.PlayerRanking;
import org.jeecg.modules.zhima.portal.service.IPlayerRankingService;
import org.jeecg.modules.zhima.portal.service.IPlayerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class PlayerRankingServiceImpl implements IPlayerRankingService {

    @Autowired
    private IZmPlayerRankingService zmPlayerRankingService;
    @Autowired
    private IPlayerService playerService;


    @Override
    public IPage<PlayerRanking> page(Page<ZmPlayerRanking> page, QueryWrapper<ZmPlayerRanking> query) {
        query.orderByDesc("integral");
        IPage<ZmPlayerRanking> data = zmPlayerRankingService.page(page, query);
        IPage<PlayerRanking> result = data.convert(this::convert);
        return result;
    }

    @Override
    public List<PlayerRanking> home() {
        List<ZmPlayerRanking> zmPlayerRankingList = zmPlayerRankingService.list(new LambdaQueryWrapper<ZmPlayerRanking>().eq(ZmPlayerRanking::getType, 1).orderByDesc(ZmPlayerRanking::getIntegral).last("limit 10"));
        if (CollectionUtil.isNotEmpty(zmPlayerRankingList)){
            return zmPlayerRankingList.stream().map(this::convert).collect(Collectors.toList());
        }
        return Collections.EMPTY_LIST;
    }

    @Override
    public List<ZmPlayerRanking> list(List<String> payerIds) {
        return zmPlayerRankingService.list(new LambdaQueryWrapper<ZmPlayerRanking>().in(ZmPlayerRanking::getPlayerId, payerIds));
    }

    @Override
    public boolean updateById(ZmPlayerRanking entity) {
        return zmPlayerRankingService.updateById(entity);
    }

    @Override
    public boolean save(ZmPlayerRanking entity) {
        entity.setIntegral(0.00);
        entity.setScore(0L);
        return zmPlayerRankingService.save(entity);
    }

    @Override
    public ZmPlayerRanking getByPlayerId(String playerId) {
        return zmPlayerRankingService.getOne(new LambdaQueryWrapper<ZmPlayerRanking>().eq(ZmPlayerRanking::getPlayerId, playerId));
    }

    @Override
    public boolean calculate() {
        int count = zmPlayerRankingService.count();
        int num = count/1000 + 1;
        for (int i = 0; i < num; i++) {
            List<ZmPlayerRanking> list = zmPlayerRankingService.list(new LambdaQueryWrapper<ZmPlayerRanking>().orderByDesc(ZmPlayerRanking::getIntegral).last("limit "+i+",1000"));
            if(CollectionUtils.isNotEmpty(list)){
                for (int j = 0; j < list.size(); j++) {
                    ZmPlayerRanking zmPlayerRanking = list.get(j);
                    Player player = playerService.getById(zmPlayerRanking.getPlayerId());
                    if (ObjectUtils.isNotEmpty(player)){
                        player.setIntegral(zmPlayerRanking.getIntegral());
                        player.setRanking(j+1);
                        playerService.updateById(player);
                    }
                }
            }
        }
        return true;
    }

    private PlayerRanking convert(ZmPlayerRanking zmPlayerRanking){
        PlayerRanking playerRanking = new PlayerRanking();
        BeanUtils.copyProperties(zmPlayerRanking, playerRanking);
        playerRanking.setScoreStr(""+((double)zmPlayerRanking.getScore()/100));
        playerRanking.setPlayer(playerService.getById(zmPlayerRanking.getPlayerId()));
        return playerRanking;
    }
}
